<template>
  <div class="login-container">
    <a-form
        :model="formState"
        :rules="formRules"
        name="login_form"
        class="login-form"
        @finish="handleLogin"
    >
      <div class="login-header">
        <h2>系统登录</h2>
      </div>

      <a-form-item
          name="username"
          label-align="left"
      >
        <a-input
            v-model:value="formState.username"
            placeholder="请输入账号"
            size="large"
        >
          <template #prefix>
            <UserOutlined class="input-icon"/>
          </template>
        </a-input>
      </a-form-item>

      <a-form-item
          name="password"
          label-align="left"
      >
        <a-input-password
            v-model:value="formState.password"
            placeholder="请输入密码"
            size="large"
        >
          <template #prefix>
            <LockOutlined class="input-icon"/>
          </template>
        </a-input-password>
      </a-form-item>

      <a-form-item>
        <!--        <a-checkbox v-model:checked="formState.remember">记住我</a-checkbox>-->
        <a-button
            type="link"
            class="forgot-password"
            @click="handleForgotPassword"
        >
          忘记密码?
        </a-button>
      </a-form-item>

      <a-form-item>
        <a-button
            type="primary"
            html-type="submit"
            size="large"
            :loading="loading"
            block
        >
          登录
        </a-button>
      </a-form-item>
    </a-form>
  </div>
</template>

<script setup>
import {message} from "ant-design-vue";
import router from "@/router/index.js";

const loading = ref(false)
const formState = reactive({
  username: '',
  password: '',
  // remember: false
})

const formRules = {
  username: [
    {required: true, message: '请输入用户名', trigger: 'blur'},
    // {min: 4, max: 16, message: '用户名长度4-16个字符', trigger: 'blur'}
  ],
  password: [
    {required: true, message: '请输入密码', trigger: 'blur'},
    // {min: 6, max: 18, message: '密码长度6-18个字符', trigger: 'blur'}
  ]
}

const handleLogin = async () => {
  loading.value = true
  try {
    // 这里替换为实际的登录逻辑
    await new Promise(resolve => setTimeout(resolve, 1000))
    message.success('登录成功')
    // 登录成功后的跳转逻辑
    router.push('/home')
  } catch (error) {
    message.error(error.message || '登录失败')
  } finally {
    loading.value = false
  }
}

const handleForgotPassword = () => {
  message.destroy()
  message.info('请联系管理员重置密码')
}
</script>

<style lang="scss" scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #108000, #0a5a00);
  padding: 20px;

  .login-form {
    width: 100%;
    max-width: 420px;
    padding: 40px 30px;
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);

    .login-header {
      text-align: center;
      margin-bottom: 30px;

      h2 {
        color: #108000;
        font-size: 24px;
        font-weight: 500;
      }
    }

    .input-icon {
      color: rgba(0, 0, 0, 0.25);
    }

    .forgot-password {
      float: right;
    }

    :deep(.ant-form-item-label) {
      padding-bottom: 8px;
    }

    :deep(.ant-btn-primary) {
      background-color: #108000;
      border-color: #108000;

      &:hover {
        background-color: #0a5a00;
        border-color: #0a5a00;
      }
    }
  }
}
</style>